package com.yanqu.road.server.manager.activity.sonhai.handler.impl;

import com.yanqu.road.entity.activity.sonhai.config.SonHaiConfig;
import com.yanqu.road.entity.activity.sonhai.config.SonHaiEventConfig;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiBeastData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiBeastGroupData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPlotData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiBattleLogTypeEnum;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiBattleResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiWinPlotResp;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.handler.IPlotTypeHandler;
import com.yanqu.road.server.manager.activity.sonhai.handler.entity.PlotTypeHandlerExploreReq;
import com.yanqu.road.server.manager.activity.sonhai.handler.entity.PlotTypeHandlerExploreResp;

/**
 * 瑶碧丘（狰巢）
 */
public class PlotType6Handler implements IPlotTypeHandler {

    /**
     * 探索后处理
     * @param req
     * @return
     */
    @Override
    public PlotTypeHandlerExploreResp explore(PlotTypeHandlerExploreReq req) {
        CrossSonHaiUser user = req.getUser();
        SonHaiRegionData regionData = user.getRegionData();
        SonHaiPlotData plotData = req.getPlotData();
        CrossSonHaiActivity activity = req.getActivity();
        CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
        PlotTypeHandlerExploreResp resp = new PlotTypeHandlerExploreResp();
        SonHaiConfig config = activity.getSonHaiConfig();
        SonHaiEventConfig eventConfig = config.getEventConfig(plotData.getPlotId());
        //地块添加异兽
        int monsterId = eventConfig.randomMonsterId();
        if (monsterId <= 0) {
            activity.getLogger().error("山海伏兽-探索-兽群：异兽配置错误，{},{}", user.getActivityId(), eventConfig.getMonster());
            return null;
        }
        SonHaiBeastData beastData = activity.createBeast(monsterId, regionData.getRegionId());//创建异兽
        plotData.setBeastData(beastData);
        //是否直接镇伏地块
        if (req.isAutoWin()) {
            //原寻踪进度
            int oldProgress = regionData.getProgress();
            //直接镇伏该地块
            SonHaiWinPlotResp winPlotResp = user.winPlot(plotData);
            //获得积分
            long addScore = user.addScoreByPlot(regionData, plotData, oldProgress);
            winPlotResp.setAddScore(addScore);
            //添加山海灵气
            long baseAddAuraScore = winPlotResp.getAddAuraScore();
            int auraScoreOpRate = user.getAuraScoreOpRate(plotData, true);//buff:山海灵气加成率
            long auraScoreOpValue = user.getAuraScoreOpValue(plotData, true);//buff:山海灵气加成值
            long addAuraScore = user.countAureScore(baseAddAuraScore, auraScoreOpRate, auraScoreOpValue);
            winPlotResp.setAddAuraScore(addAuraScore);
            if (addAuraScore > 0) {
                team.changeAuraScore(user, addAuraScore);
            }
            resp.setWinPlotResp(winPlotResp);
            //日志
            if (eventConfig.getLevel() >= config.getLogPveLevel()) {
                activity.addBattleLog(
                        plotData.getPlotId(),
                        plotData.getUserId(),
                        0,
                        SonHaiBattleLogTypeEnum.PVE.getType(),
                        null,
                        winPlotResp.getReward(),
                        addScore,
                        addAuraScore,
                        0);
            }
        }
        return resp;
    }
}
